﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<assert.h>
using namespace std;
struct Point
{
	int _x;
	int _y;
};
class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1)
        :_year(year)
        , _month(month)
        , _day(day)
    {
        cout << "Date(int year, int month, int day)" << endl;
    }
    Date(const Date& d)
        :_year(d._year)
        , _month(d._month)
        , _day(d._day)
    {
        cout << "Date(const Date& d)" << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
//int main()
//{
//	int array[]{ 1,2,34 };
//	int array1[3]{ 0 };
//	Point s1{ 1,2 };
//     C++98 ⽀持的
//
//        int a1[] = { 1, 2, 3, 4, 5 };
//    int a2[5] = { 0 };
//    Point p = { 1, 2 };
//
//
//        Date d1 = { 2025, 1, 1 };
//    
//   
//
//        const Date& d2 = { 2024, 7, 25 };
//   
//    Date d3 = { 2025 };
//    Date d4 = 2025;
//  
//        Point p1{ 1, 2 };
//    int x2{ 2 };
//    Date d6{ 2024, 7, 25 };
//    const Date& d7{ 2024, 7, 25 };
//      不⽀持，只有  {}      初始化，才能省略        =
//         Date d8 2025;
//        vector<Date> v;
//    v.push_back(d1);
//    v.push_back(Date(2025, 1, 1));
//       ⽐起有名对象和匿名对象传参，这⾥  {}   更有性价⽐
//
//        v.push_back({ 2025, 1, 1 });
//    
//	return 0;
//}
using namespace std;
void f(int& x)
{
    std::cout << "左值引⽤重载     f(" << x << ")\n";
}
void f(const int& x)
{
    std::cout << "   到 const的左值引⽤重载f(" << x << ")\n";
}
void f(int&& x)
{
    std::cout << " 右值引⽤重载 f(" << x << ")\n";
}
//int main()
//{
//    int d = 2;
//    int c = 2;
//    int&& a = c+d;
//    int* p = new int(0);
//    int b = 1;
//    const int dc = b;
//    *p = 10;
//    string s("111111");
//    s[0] = 'x';
//    double x = 1.1, y = 2.2;
//    int i = 1;
//    const int ci = 2;
//    f(i);
//    f(ci);
//    f(3);
//    f(std::move(i)); 
//    return 0;
//
//}
namespace mutou
{
    class string
    {
    public:
        typedef char* iterator;
        typedef const char* const_iterator;
        iterator begin()
        {
            return _str;
        }
        iterator end()
        {
            return _str + _size;
        }
        const_iterator begin() const
        {
            return _str;
        }
        const_iterator end() const
        {
            return _str + _size;
        }
        string(const char* str = "")
            :_size(strlen(str))
            , _capacity(_size)
        {
            cout << "string(char* str)  构造  " << endl;
                _str = new char[_capacity + 1];
            strcpy(_str, str);
        }
        void swap(string& s)
        {
            ::swap(_str, s._str);
            ::swap(_size, s._size);
            ::swap(_capacity, s._capacity);
        }
        string(const string& s)
            :_str(nullptr)
        {
            cout << "string(const string& s) --    拷⻉构造 " << endl;
            reserve(s._capacity);
            for (auto ch : s)
            {
                push_back(ch);
            }
        }

        //  移动构造

            string(string&& s)
        {
            cout << "string(string&& s) --   移动构造 " << endl;
                swap(s);
        }
        string& operator=(const string& s)
        {
            cout << "string& operator=(const string& s) --   拷⻉赋值 " <<   endl;
            if (this != &s)
            {
                _str[0] = '\0';
                _size = 0;
                reserve(s._capacity);
                for (auto ch : s)
                {
                    push_back(ch);
                }
            }
            return *this;
        }

        //  移动赋值

            string& operator=(string&& s)
        {
            cout << "string& operator=(string&& s) -- 移动赋值 " << endl;
                swap(s);
            return *this;
        }
        ~string()
        {
            cout << "~string() --  析构 " << endl;
                delete[] _str;
            _str = nullptr;
        }
        char& operator[](size_t pos)
        {
            assert(pos < _size);
            return _str[pos];
        }
        void reserve(size_t n)
        {
            if (n > _capacity)
            {
                char* tmp = new char[n + 1];
                if (_str)
                {
                    strcpy(tmp, _str);
                    delete[] _str;
                }
                _str = tmp;
                _capacity = n;
            }
        }
        void push_back(char ch)
        {
            if (_size >= _capacity)
            {
                size_t newcapacity = _capacity == 0 ? 4 : _capacity *
                    2;
                reserve(newcapacity);
            }
            _str[_size] = ch;
            ++_size;
            _str[_size] = '\0';
        }
        string& operator+=(char ch)
        {
            push_back(ch);
            return *this;
        }
        const char* c_str() const
        {
            return _str;
        }
        size_t size() const
        {
            return _size;
        }
    private:
        char* _str = nullptr;
        size_t _size = 0;
        size_t _capacity = 0;
    };

}
namespace bit
{
    template<class T>
    struct ListNode
    {
        ListNode<T>* _next;
        ListNode<T>* _prev;
        T _data;
        ListNode(const T& data = T())
            :_next(nullptr)
            , _prev(nullptr)
            , _data(data)
        {
        }
        ListNode(T&& data)
            :_next(nullptr)
            ,_prev(nullptr)
            ,_data(std::move(data))
        {
           
        }
        template<class ...Arg>
        ListNode(Arg&&...arg)
            :_next(nullptr)
            ,_prev(nullptr)
            ,_data(forward<Arg>(arg)...)
        {

        }
    };
    template<class T, class Ref, class Ptr>
    struct ListIterator
    {
        typedef ListNode<T> Node;
        typedef ListIterator<T, Ref, Ptr> Self;
        Node* _node;
        ListIterator(Node* node)
            :_node(node)
        {
        }
        Self& operator++()
        {
            _node = _node->_next;
            return *this;
        }
        Ptr operator->()
        {
            return &(_node->_data);
        }
        Ref operator*()
        {
            return _node->_data;
        }
        bool operator!=(const Self& it)
        {
            return _node != it._node;
        }
    };
    template<class T>
    class list
    {
        typedef ListNode<T> Node;
    public:
        typedef ListIterator<T, T&, T*> iterator;
        typedef ListIterator<T, const T&, const T*> const_iterator;
        iterator begin()
        {
            return iterator(_head->_next);
        }
        iterator end()
        {
            return iterator(_head);
        }
        void empty_init()
        {
            _head = new Node();
            _head->_next = _head;
            _head->_prev = _head;
        }
        list()
        {
            empty_init();
        }
        void push_back(const T& x)
        {
            insert(end(), x);
        }
        void push_back(T&& x)
        {
            insert(end(), move(x));
        }
        template<class ...Arg>
        void emplace_back(Arg&&...arg)
        {
            emplace(end(), forward<Arg>(arg)...);
        }
        template<class ...Arg>
        iterator emplace(iterator pos, Arg&&... x)
        {
            Node* cur = pos._node;
            Node* newnode = new Node(forward<Arg>(x)...);
            Node* prev = cur->_prev;
            // prev  newnode  cur
            prev->_next = newnode;
            newnode->_prev = prev;
            newnode->_next = cur;
            cur->_prev = newnode;
            return iterator(newnode);
        }
       
       template<class X>
        iterator insert(iterator pos, X && x)
        {
            Node* cur = pos._node;
            Node* newnode = new Node(forward<X>(x));
            Node* prev = cur->_prev;
            // prev  newnode  cur
            prev->_next = newnode;
            newnode->_prev = prev;
            newnode->_next = cur;
            cur->_prev = newnode;
            return iterator(newnode);
        }
    private:
        Node* _head;
    };
}
int main()
{
    //mutou::string s1("xxxxx");
    ////  拷⻉构造

    //    mutou::string s2 = s1;
    //// 构造 + 移动构造，优化后直接构造

    //    mutou::string s3 = mutou::string("yyyyy");
    ////  移动构造

    //    mutou::string s4 = move(s1);
    //cout << "******************************" << endl;
 /*   std::list<mutou::string> lt;
    mutou::string s1("111111111111111111111");
    lt.push_back(s1);
    cout << "*************************" << endl;
    lt.push_back(mutou::string("22222222222222222222222222222"));
    cout << "*************************" << endl;
    lt.push_back("3333333333333333333333333333");
    cout << "*************************" << endl;
    lt.push_back(move(s1));
    cout << "*************************" << endl;*/
    bit::list<mutou::string> lt;
    cout << "*************************" << endl;
    mutou::string s1("111111111111111111111");
    lt.push_back(s1);
    cout << "*************************" << endl;
    lt.emplace_back(mutou::string("22222222222222222222222222222"));
    cout << "*************************" << endl;
    lt.emplace_back("dadadadad");
    bit::list<pair<string, int>> llt;
    llt.emplace_back("dadadaddad", 1);
    auto it = llt.begin();
    cout << it->first << it->second << endl;
    return 0;
}
